import axios from 'axios'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
// import store from '@/store'

const service = axios.create({
    baseURL: "/api", //基础路径
    timeout: 20000, //请求超时
})

export default service

//请求拦截器
service.interceptors.request.use(
    (config) => {
        Nprogress.start()
        let userTempId = store.state.user.userTempId
        if (userTempId) config.headers.userTempId = userTempId
        return config;
    }, error => {
        return error.message
    }
)

//响应拦截器
service.interceptors.response.use(
    response => {
        Nprogress.done()
        return response.data
    },
    error => {
        Nprogress.done()
        alert('请求失败' + error.message || '未知错误')
        return new Promise(() => {})
    }
)